package cn.tedu.controller;


import cn.tedu.pojo.User;
import cn.tedu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {


    @Autowired
    UserService userService;

    @ResponseBody
    @PostMapping("/login")
    public String login(@RequestBody User user, HttpServletRequest req){
        System.out.println(user);
        User user1 = userService.getUser(user.getName());
        if(user1 !=null){
            if(user1.getPassword().equals(user1.getPassword())){
                req.getSession().setAttribute("user",user1);
                return "1";
            }
        }
        return "";
    }

    @ResponseBody
    @PostMapping(value = "/register",produces = {"text/html;charset=UTF-8;"})
    public String register(@RequestBody User user){
        System.out.println(user);
        int i = userService.insertUser(user);
        if(i!=0){
            return "注册成功";
        }
        return "注册失败";
    }

}
